/*****************************************************************************
* file  : key.c
*******************************************************************************/
#include "mytype.h"
#include "gui_main.h"
#include "pincfg.h"
#include "os_obj.h"
#include "framework.h"
#include "par\par_tab.h"

key_click_type click_key;
extern par_manager_type par_set;

volatile uint8_t click_key_cnt;
volatile uint8_t click_found_cnt;
volatile uint8_t click_lost_cnt;
timer_pulse_type key_pulse={0};
extern const key_api_type key_api;
extern const par_jump_list_type par_tab_jump[];

/* key read
* 
*/
void key_scan(void)
{
    static char key_scnt = 0;
    uint16_t key_read = 0;
    
    key_scnt++;
    if(key_scnt < 5)
        return;
    
    key_scnt = 0;
    
	if(key1.in_st())
	key_read |= 1;
	if(key2.in_st())
	key_read |= 2;
	if(key3.in_st())
	key_read |= 4;
	if(key4.in_st())
	key_read |= 8;
	
    key8_st_sample(&gui_st.key, key_read);
}

/*****************************************************************************
*
* @brief   storage task.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void key_task(void const *argument)
{ 
	app_timer_task_add(key_scan);
	
    pulse_init(&key_pulse, app_tick_get());
    while(1)
    {
        osDelay(ms_ticks(50));
		pulse_creat(&key_pulse, app_tick_get());

		par_set.jump_list = 0;
		gui_st.key.api = &key_api;
		key8_st_handle(&gui_st.key, key_pulse.pulse_100ms);
		
        click_key.space_sv = 6;

        key_click_handle(&click_key, (0!=(gui_st.key.val.uval&KEY_VAL_SET)) ,1);
        
        if(click_key.multi_click_out)
        {
            click_key_cnt++;
        }
        if(click_key.edge_found)
        {
            click_found_cnt++;
        }
        if(click_key.edge_lost)
        {
            click_lost_cnt++;
        }
    }
}

